package console4.widgets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;


public class ConsoleMainWindow {
		
		private CTabFolder tabbedFolder;
		private LinkedList<ConsoleTab> consoles=new LinkedList<ConsoleTab>();

		
		
		public ConsoleMainWindow(Shell shell) {
			generateGUI(shell);
		}
		
		protected List<ConsoleTab> getConsoles() {
			return consoles;
		}

		private void generateGUI(Shell shell) {
			shell.setLayout(new FillLayout());
			tabbedFolder = new CTabFolder(shell,SWT.BORDER);
			tabbedFolder.setSimple(false);
			tabbedFolder.setUnselectedCloseVisible(true);
			tabbedFolder.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					((ConsoleTab)e.item).resetFocus();
				}
			});
			Button b=new Button(tabbedFolder,SWT.PUSH);
			b.setText("*");
			b.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					createNewTab(tabbedFolder);
					tabbedFolder.setSelection(tabbedFolder.getItemCount()-1);
				}
			});
			tabbedFolder.setTopRight(b);
		}

		private void createNewTab(CTabFolder tabbedFolder) {
			ConsoleTab ct;
			try {
				ct = new ConsoleTab(tabbedFolder,"CMD "+(tabbedFolder.getItems().length+1));
				consoles.add(ct);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		private void createNewTab(TabSaveState tss) {
			try {
				ConsoleTab ct = new ConsoleTab(tabbedFolder,"CMD "+(tabbedFolder.getItems().length+1),tss);
				consoles.add(ct);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		public void resetFocus() {
			if(tabbedFolder.getSelection()!=null)
				((ConsoleTab)tabbedFolder.getSelection()).resetFocus();
		}

		public void loadTabs() {
			File f=new File("tabs.se");
			if(f.exists()) {
				ObjectInputStream fis = null;
				try {
					fis = new ObjectInputStream(new GZIPInputStream(new FileInputStream(f)));
					List<TabSaveState> saves=(List<TabSaveState>) fis.readObject();
					for(TabSaveState tss:saves) {
						createNewTab(tss);
					}
				}
				catch ( Exception e ) {e.printStackTrace();}
				finally { try { fis.close(); } catch ( Exception e ) { e.printStackTrace(); } }
			}
			if(tabbedFolder.getItemCount()==0)
				createNewTab(tabbedFolder);
		}

		public void saveTabs() {
			ObjectOutputStream fos = null;
			try
			{
			  fos = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream("tabs.se")));
			  LinkedList<TabSaveState> saves=new LinkedList<TabSaveState>();
			  for(CTabItem i:tabbedFolder.getItems())
				  saves.add(((ConsoleTab)i).createSaveState());
			  fos.writeObject(saves);
			}
			catch ( IOException e ) {e.printStackTrace();}
			finally { try { fos.close(); } catch ( Exception e ) { e.printStackTrace(); } }
		}
}
